"""
数据存储管理类
负责本地文件系统的数据读写操作
"""

import json
import os
import logging
from typing import Dict, Any

logger = logging.getLogger(__name__)


class DataStore:
    """本地文件系统数据存储管理器"""
    
    @staticmethod
    def load_data(file_path: str) -> Dict[str, Any]:
        """从JSON文件加载数据"""
        if os.path.exists(file_path):
            try:
                with open(file_path, 'r', encoding='utf-8') as f:
                    return json.load(f)
            except Exception as e:
                logger.error(f"加载数据失败 {file_path}: {e}")
                return {}
        return {}
    
    @staticmethod
    def save_data(file_path: str, data: Dict[str, Any]) -> bool:
        """保存数据到JSON文件"""
        try:
            with open(file_path, 'w', encoding='utf-8') as f:
                json.dump(data, f, ensure_ascii=False, indent=2)
            return True
        except Exception as e:
            logger.error(f"保存数据失败 {file_path}: {e}")
            return False
    
    @staticmethod
    def backup_data(file_path: str) -> bool:
        """备份数据文件"""
        if not os.path.exists(file_path):
            return False
        
        try:
            backup_path = f"{file_path}.backup"
            with open(file_path, 'r', encoding='utf-8') as src:
                with open(backup_path, 'w', encoding='utf-8') as dst:
                    dst.write(src.read())
            logger.info(f"数据备份成功: {backup_path}")
            return True
        except Exception as e:
            logger.error(f"数据备份失败: {e}")
            return False
